//
//  HDIndividualInformationVC.swift
//  HuaDaoPeifu
//
//  Created by liangyuanliangyuan on 16/4/19.
//  Copyright © 2016年 paiyipai. All rights reserved.
//

import UIKit

protocol backPassImageDelegate :class{
    func backPassImage(_ img:UIImage)
}

class HDIndividualInformationVC: UITableViewController {

    let systemPhoto = PYPSystemCameraPhotoControl()
    
    
    var transImage = UIImageView()
    var userInfoModel = HDAccountInfo()
    var passName: String?
    var passPhone: String?
    var passUserType: String?
    var passCritCode: String?
    var passCritType: String?
    var passHeadImg: UIImage?
    var passCrittype_info:  String?
    var passInsuredgrade:  Int?
    var delegate:  backPassImageDelegate?
    var userModel:PYPUserInfoRespond?
    var numberOfRow: Int?
    override init(style: UITableViewStyle) {
        super.init(style: style)
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewWillAppear(_ animated: Bool) {
        navigationController?.navigationBar.isHidden = false

        tableView.reloadData()
    }
    override func viewDidDisappear(_ animated: Bool) {
        if transImage.image != nil {
            transImage.image = nil
        }
    }
    override func viewDidLoad() {
        super.viewDidLoad()

        title = "个人信息"
        self.tableView.tableFooterView = UIView(frame: CGRect.zero)
        self.tableView.isScrollEnabled = false
//        tableView.separatorStyle = .None
        tableView.backgroundColor = UIColor(hex: 0xf8f8f8)
        tableView.separatorColor = UIColor(hex: 0xdbdbdb)
//       [self.tableView setSeparatorColor:[UIColor clearColor]];
        
    }
    
    func setColorOfLabel(_ textLabel:UILabel)->UILabel{
        
        let noteStr = NSMutableAttributedString(string: textLabel.text!)
        let colorRange = NSMakeRange(11,(textLabel.text?.characters.count)! - 11)
        noteStr.addAttribute(NSForegroundColorAttributeName, value: ConstantColor.appThemeColor, range: colorRange)
        textLabel.attributedText = noteStr
        
        return textLabel
        
        
    }
    
    override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let headView = UIView(frame: CGRect(x: 0,y: 0,width: AppConstant.screenWidth,height: 10))
        headView.backgroundColor = UIColor.clear
        return headView
    }
    

    override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 10.0
    }
    override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0.5
    }
    override func numberOfSections(in tableView: UITableView) -> Int {
        
        return 2
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            if section == 0 {
                return 1
            }else{
//                if passUserType == "0" {
//                    numberOfRow = 4
//                    return 4
//                }else {
                    numberOfRow = 6
                    return 6
//                }
            }
    }

    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if indexPath.section == 0 {
            return 70
        }
        return 49.0
    }
    
  
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
            if indexPath.section == 0 {
                let reuseID = "HDIndividualInformationCell2"
                let cell0 = HDIndividualInformationCell2(style: .default, reuseIdentifier: reuseID)
                cell0.headText.text = "头像"
                if passHeadImg == nil  {
                    cell0.headImage.image = UIImage(named: "个人中心默认头像")
                }else{
                    if transImage.image == nil  {
                       cell0.headImage.image = passHeadImg
                        
                    }else {
                        cell0.headImage.image = transImage.image
                    }
                }
                cell0.selectionStyle = .none
                return cell0
            } else {
                
                if numberOfRow == 4 {
                    let reuseID = "HDIndividualInformationCell"
                    let cell = HDIndividualInformationCell(style: .default, reuseIdentifier: reuseID)
                    cell.refreshNormalCell(indexPath)
                    cell.selectionStyle = .none
                    if indexPath.row == 0 {
                        cell.rightTitle.text = passName
                    }else if indexPath.row == 1 {
                        cell.rightTitle.text = passPhone! + "   重新绑定"
                        cell.rightTitle = setColorOfLabel(cell.rightTitle)

                    }else if indexPath.row == 2 {
                        cell.rightTitle.text = passCrittype_info
                    }else if indexPath.row == 3 {
                        cell.rightTitle.text = passCritCode
                    }
                    cell.selectionStyle = .none
                    return cell
                }else {
                    if indexPath.row == 5 {
                        let reuseID = "bankcell"
                        let cell1 = HDIndividualInformationCell2(style: .default, reuseIdentifier: reuseID)
                        cell1.headImage.isHidden = true
                        cell1.headText.text = "银行信息"
                        
                        cell1.selectionStyle = .none
                        return cell1
                    }else{
                        let reuseID = "HDIndividualInformationCell"
                        let cell = HDIndividualInformationCell(style: .default, reuseIdentifier: reuseID)
                        cell.refreshCompanyCell(indexPath)
                        
                        if indexPath.row == 0 {
                            cell.rightTitle.text = passName
                        }else if indexPath.row == 1 {
                            cell.rightTitle.text = passPhone! + "   重新绑定"
                            cell.rightTitle = setColorOfLabel(cell.rightTitle)
                        }else if indexPath.row == 2 {
                            cell.rightTitle.isHidden = true
                            cell.rightImage.isHidden = false
                            if passInsuredgrade! == 0 {
                                cell.rightImage.image = UIImage(named: "普通")
                            }else if passInsuredgrade! == 1 {
                                cell.rightImage.image = UIImage(named: "vip")
                            }
                        }else if indexPath.row == 3 {
                            cell.rightTitle.text = passCrittype_info
                        }else if indexPath.row == 4 {
                            cell.rightTitle.text = passCritCode
                        }
                        cell.selectionStyle = .none
                        return cell
                    }
                }
            }
    }
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if indexPath.section == 0 {

            systemPhoto.editAvatar(self)
        }else if indexPath.section == 1 && indexPath.row == 5 {
            let bankVC = HDBankInformationVC()
            navigationController?.pushViewController(bankVC, animated: true)
        }else if((indexPath as IndexPath).section == 1 && (indexPath as IndexPath).row == 1){
            let contr = HDChangePhoneViewController()
            contr.phoneNumberStr = passPhone!
            contr.oldPhoneNumber = userInfoModel.tphone!
            navigationController?.pushViewController(contr, animated: true)
            
        }
    }
    
}

extension HDIndividualInformationVC: PYPSystemCameraPhotoControlDelegate {
    func didFinishPickingMediaWithInfo(_ info: [String : AnyObject]) {
        let image = info[UIImagePickerControllerEditedImage] as? UIImage
        transImage.image = image!
        HDAccountHandler.saveUserHeadImage(image!)
        delegate?.backPassImage(transImage.image!)
        print(HDAccountHandler.token)
        HDApiManagerHandler.requestUploadUserHeadImg(HDAccountHandler.token!, headimg: image!, success: { (dict) in

            let status : Int? = dict.object(forKey: "status") as? Int
            
            if status == 1 {
                HDAccountHandler.saveUserHeadImage(image!)
            }
            
        }) { (error) in
                
        }
    }
}


